package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.ObjectUtil;
import com.budwk.app.sqfw.models.WeatherArea;
import com.budwk.app.sqfw.services.WeatherAreaService;
import com.budwk.app.sqfw.vo.Sqfw_formSummaryForAdminVO;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.WeatherUtil;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiFormParams;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.*;

import java.util.Arrays;

@IocBean
@At("/platform/sqfw/weather")
@ApiDefinition(tag = "天气预报")
@Slf4j
public class WeatherController {
    @Inject
    private WeatherAreaService weatherAreaService;


    @Inject
    private SysUserService sysUserService;

    @At()
    @Ok("json")
    @GET
    @SaCheckLogin
    public Result<?> execute() {
        Sys_user user =  sysUserService.fetch(SecurityUtil.getUserId());
        if("直属".equals(user.getDistrict())){
            user.setDistrict(user.getCity());
        }
        WeatherArea weatherArea = weatherAreaService.fetch(Cnd.where(WeatherArea::getName,"like",user.getDistrict().substring(0,2)+"%"));
        if(ObjectUtil.isEmpty(weatherArea)){
            weatherArea =new WeatherArea();
            weatherArea.setCode("101260101");
        }
        return Result.data( WeatherUtil.execute(Arrays.asList(Integer.valueOf(weatherArea.getCode()))).get(0));
    }

    @At()
    @Ok("json")
    @GET
    @SaCheckLogin
    public Result<?> sevenDays() {
        Sys_user user =  sysUserService.fetch(SecurityUtil.getUserId());
        if("直属".equals(user.getDistrict())){
            user.setDistrict(user.getCity());
        }
        WeatherArea weatherArea = weatherAreaService.fetch(Cnd.where(WeatherArea::getName,"like",user.getDistrict().substring(0,2)+"%"));
        if(ObjectUtil.isEmpty(weatherArea)){
            weatherArea =new WeatherArea();
            weatherArea.setCode("101260101");
        }
        return Result.data( NutMap.NEW().addv("list",WeatherUtil.getResult(Arrays.asList(Integer.valueOf(weatherArea.getCode()))).get(0)));
    }
}
